﻿@page "/abnormalr/index"
@attribute [Authorize]
@using Microsoft.AspNetCore.Authorization;
@using TMV.Application.Abnormal.Servives;
@using TMV.DTO;
@using TMV.DTO.Abnormal;
@using TMV.Web.Pages.Abnormalr.ViewModel;
@inherits ProCompontentBase
@inject NavigationManager Nav
@inject IAbnormalServiceDM _arServiceDM
<MCard Class="mt-6 full-card">
	<MCardText Class="pa-6">
		<MRow>

			<MCol Md=2 Sm=2 Class="d-flex block-center">

				<MTextField @bind-Value="tpPage.Code" Color="primary" Width="120" Class="rounded-2" Type="number" Clearable HideDetails="@("auto")" Flat Dense Solo BackgroundColor="fill-lighten-1" Placeholder="编号">
				</MTextField>
				</MCol>
				<MCol Md=9 Sm=2 Class="d-flex block-center">
				<MButton Color="primary" MinWidth=80 Height=32 OnClick="()=>seach()">
					查询
				</MButton>
				</MCol>
				
				<MCol Md=1 Sm=12 Class="d-flex block-center">
				<MButton Color="primary" MinWidth=80 Height=32 OnClick="()=>_visible=true">
					新增
				</MButton>
				</MCol>
				</MRow>
				</MCardText>
				<MDataTable Headers="_headers" Items=" tpPage.Datas" TItem="AbnormalDTO" ItemsPerPage="tpPage.PageSize" OnOptionsUpdate="HandleOnOptionsUpdate"
				ServerItemsLength="tpPage.PageCount"
				Loading="_loading">
		<HeaderColContent Context="header">
			<span class="text-subtitle">@header.Text</span>
		</HeaderColContent>
		<ItemColContent>
			@switch (context.Header.Value)
			{
				case "actions":
					<MIcon Small Class="mr-2" OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
					<MIcon Small OnClick="async()=>await DeleteItem(context.Item)">mdi-delete</MIcon>
					break;
				default:
					@context.Value
					break;
			}

		</ItemColContent>
	</MDataTable>
	<MDialog @bind-Value="_visible"
			 MaxWidth="900">
		<MCard>
			<MForm @bind-Value="_valid" Model="_editedItem" @ref="_form" EnableValidation>
			<MCardTitle>
				<span class="text-h5">@FormTitle</span>
			</MCardTitle>

			<MCardText>
				<MContainer>
					<MRow>
						<MCol Cols="12" Sm="6" Md="12">
								<MTextField @bind-Value="_editedItem.Code" Clearable Label="编号" required HideDetails="true"></MTextField>
						</MCol>
						
						</MRow>
						<MRow>
							<MCol Cols="12" Sm="6" Md="12">
								<MTextarea ClearIcon="mdi-close-circle" required HideDetails="true"
								   Label="异常原因"
								   @bind-Value="_editedItem.AbnormalCause">
							</MTextarea>
								</MCol>
						</MRow>
						<MRow>
							<MCol Cols="12" Sm="6" Md="12">
								<MTextarea ClearIcon="mdi-close-circle" required HideDetails="true"
							   Label="处理方式"
							   @bind-Value="_editedItem.Disposal">
							  
					</MTextarea>
								</MCol>
					</MRow>
				</MContainer>
			</MCardText>
			<MCardActions>
				<MSpacer></MSpacer>
				<MButton Color="blue darken-1" Text OnClick="Close">取消</MButton>
				<MButton Color="blue darken-1" Text OnClick="Save">保存</MButton>
				</MCardActions>
				</MForm>
				</MCard>
				</MDialog>
				</MCard>
					@code {
	private int _editedIndex = -1;
	private bool _loading = true;
	bool _valid = false;
	private MForm _form;
	private DataOptions _options = new DataOptions();
	public class RoleItem
	{
		public string Label { get; set; }
		public string Value { get; set; }
		public RoleItem(string label, string value)
		{
			Label = label;
			Value = value;
		}
	}
	public string FormTitle
	{
		get
		{
			return _editedIndex == -1 ? "新增" : "编辑";
		}
	}
	public AbnormalDTO _editedItem = new AbnormalDTO();
	List<DataTableHeader<AbnormalDTO>> _headers = new()
		{
			new() { Text = "编号", Value = nameof(AbnormalDTO.Code), CellClass = "" },
			new() { Text = "异常原因", Value = nameof(AbnormalDTO.AbnormalCause) },
			new() { Text = "处理方式", Value = nameof(AbnormalDTO.Disposal) },
			new() { Text = "操作", Value = "actions" }

		};
	bool _visible = false;
	public List<int> _pageSize = new() { 10, 20, 50 };
	public AbnormalPage tpPage = new();
	protected override void OnInitialized()
	{
		//_editedItem.ArrivalTime = DateTime.Now;
		Initialize();
	}
	public void Initialize()
	{
		_loading = true;
		var dto = new Request_Abnormal();
		if (!string.IsNullOrWhiteSpace(tpPage.Code)) dto.Code = tpPage.Code;
		if (_options.ItemsPerPage > 0)
		{
			dto.PageSize = _options.ItemsPerPage;
		}
		if (_options.ItemsPerPage < 0)
		{
			dto.PageSize = 1000000;
		}
		dto.PageIndex = _options.Page == 0 ? 1 : _options.Page;
		var rest = _arServiceDM.GetAbnormalList(dto);
		var result = new AbnormalPage(rest.Data) { PageIndex = rest.PageIndex, PageSize = rest.PageSize, PageCount = rest.Count };
		tpPage = result;
		_loading = false;
		InvokeAsync(StateHasChanged);
	}
	public void HandleOnOptionsUpdate(DataOptions options)
	{
		_options = options;
		Initialize();
	}
	public void EditItem(AbnormalDTO abnormalRecordsModel)
	{
		//_editedItem = usersDTO;
		_visible = true;
		_editedIndex = 1;
		_editedItem.Id = abnormalRecordsModel.Id;
		_editedItem.Disposal = abnormalRecordsModel.Disposal;
		//_date = DateOnly.FromDateTime(_editedItem.ArrivalTime);
		// selectvalue = usersDTO.Type.ToString();
	}
	public async Task DeleteItem(AbnormalDTO abnormalDTO)
	{
		var result = _arServiceDM.DeAbnormal(abnormalDTO.Id);
		if (result.IsSuccess)
		{
			await PopupService.ToastAsync("删除成功", AlertTypes.Success);
			tpPage.Datas.RemoveAt(tpPage.Datas.FindIndex(u => u.Id == abnormalDTO.Id));
		}
		else
		{
			await PopupService.ToastAsync(result.Msg, AlertTypes.Error);
		}
	}
	public void Close()
	{
		_visible = false;
		_editedItem = new AbnormalDTO();
		_editedIndex = -1;
	}
	public async Task Save()
	{
		ResultEntity<bool> resultEntity = new();
		var bol = _form.Validate();
		if (bol)
		{
			var model = _editedItem.Adapt<AbnormalModel>();
			if (_editedIndex > -1)
			{
				var item = tpPage.Datas.Any(x => x.Id == _editedItem.Id);
				if (!item) { await PopupService.ToastAsync("数据不存在", AlertTypes.Warning); return; }
				resultEntity = _arServiceDM.UpAbnormal(model);
			}
			else
			{
				resultEntity = _arServiceDM.AddAbnormal(model);

			}
			if (resultEntity.IsSuccess)
			{
				//_editedItem.AddTime = DateTime.Now;
				await PopupService.ToastAsync("操作成功", AlertTypes.Success);
				if (_editedIndex == -1) tpPage.Datas.Add(_editedItem);
			}
			else
			{
				await PopupService.ToastAsync(resultEntity.Msg, AlertTypes.Error);
			}
			Initialize();
			Close();
		}
		
	}
	public void OnFiltChange(string newValue)
	{
		tpPage.Search = newValue;
		Initialize();
	}
	public void seach()
	{
		Initialize();
	}
}

